Skip to content

Register ping handler in McpSessionHandler to support server-initiated pings#1391

Merged
stephentoub merged 4 commits intomainfrom
copilot/fix-register-ping-handler
Feb 26, 2026
Merged

Register ping handler in McpSessionHandler to support server-initiated pings#1391
stephentoub merged 4 commits intomainfrom
copilot/fix-register-ping-handler

Conversation

Copy link
Contributor

Copilot AI commented Feb 25, 2026

Per the MCP spec, ping can be initiated by either party. McpClientImpl never registered a handler for the ping method, causing server→client pings to fail with -32601 MethodNotFound.

Changes

  • McpSessionHandler: Unconditionally registers a ping request handler in the constructor, ensuring all sessions (both client and server) automatically respond to ping requests with an empty PingResult. This centralizes the behavior in one place rather than duplicating it across client and server.
  • McpServerImpl: Removed ConfigurePing() method and its call from the constructor, as ping is now handled by McpSessionHandler.
  • McpClientTests: Adds ServerCanPingClient test that sends a ping from the server to the connected client and asserts a successful response.
Original prompt

Bug

Per the MCP specification, ping requests may be initiated by either party (client→server or server→client), and the receiver must respond with an empty {} result. See: https://modelcontextprotocol.io/specification/2025-03-26/basic/utilities/ping

Currently, server→client pings fail with -32601 MethodNotFound because McpClientImpl never registers a handler for the ping method.

Root Cause

In src/ModelContextProtocol.Core/Server/McpServerImpl.cs, the constructor correctly calls ConfigurePing() (around line 96), which registers a handler via SetHandler(RequestMethods.Ping, ...).

In src/ModelContextProtocol.Core/Client/McpClientImpl.cs, the constructor calls RegisterHandlers() which only registers handlers for sampling/createMessage, roots/list, elicitation/create, and tasks/* methods. There is no ConfigurePing() call and no ping handler registered for the client side.

Fix

Register a ping request handler in McpClientImpl so that incoming ping requests from the server are automatically responded to, matching the behavior already present on the server side. The handler should be unconditionally registered (just like on the server side) — it simply returns a new PingResult.

In McpClientImpl.RegisterHandlers (or the constructor), add something equivalent to:

requestHandlers.Set(
    RequestMethods.Ping,
    (request, _, cancellationToken) => new ValueTask<PingResult>(new PingResult()),
    McpJsonUtilities.JsonContext.Default.JsonNode,
    McpJsonUtilities.JsonContext.Default.PingResult);

Please also add a test that verifies a server-initiated ping to the client succeeds (i.e., returns an empty result rather than throwing -32601).

Reference

This pull request was created from Copilot chat.


🔒 GitHub Advanced Security automatically protects Copilot coding agent pull requests. You can protect all pull requests by enabling Advanced Security for your repositories. Learn more about Advanced Security.

Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix missing ping request handler in McpClientImpl Register ping handler on McpClient to support server-initiated pings Feb 25, 2026
…mpl and McpClientImpl

Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
Copilot AI changed the title Register ping handler on McpClient to support server-initiated pings Register ping handler in McpSessionHandler to support server-initiated pings Feb 26, 2026
@stephentoub stephentoub requested a review from halter73 February 26, 2026 19:00
@stephentoub stephentoub merged commit 919e838 into main Feb 26, 2026
10 checks passed
@stephentoub stephentoub deleted the copilot/fix-register-ping-handler branch February 26, 2026 23:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Ping from McpServer -> McpClient is not handled by McpClient.

3 participants